package cn.iocoder.yudao.module.agri.service.farmmodel;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.agri.controller.admin.farmmodel.vo.*;
import cn.iocoder.yudao.module.agri.dal.dataobject.farmmodel.FarmModelDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.agri.convert.farmmodel.FarmModelConvert;
import cn.iocoder.yudao.module.agri.dal.mysql.farmmodel.FarmModelMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.agri.enums.ErrorCodeConstants.*;

/**
 * 种养模型 Service 实现类
 *
 * @author 广东小蜂
 */
@Service
@Validated
public class FarmModelServiceImpl implements FarmModelService {

    @Resource
    private FarmModelMapper farmModelMapper;

    @Override
    public Long createFarmModel(FarmModelCreateReqVO createReqVO) {
        // 插入
        FarmModelDO farmModel = FarmModelConvert.INSTANCE.convert(createReqVO);
        farmModelMapper.insert(farmModel);
        // 返回
        return farmModel.getId();
    }

    @Override
    public void updateFarmModel(FarmModelUpdateReqVO updateReqVO) {
        // 校验存在
        validateFarmModelExists(updateReqVO.getId());
        // 更新
        FarmModelDO updateObj = FarmModelConvert.INSTANCE.convert(updateReqVO);
        farmModelMapper.updateById(updateObj);
    }

    @Override
    public void deleteFarmModel(Long id) {
        // 校验存在
        validateFarmModelExists(id);
        // 删除
        farmModelMapper.deleteById(id);
    }

    private void validateFarmModelExists(Long id) {
        if (farmModelMapper.selectById(id) == null) {
            throw exception(FARM_MODEL_NOT_EXISTS);
        }
    }

    @Override
    public FarmModelDO getFarmModel(Long id) {
        return farmModelMapper.selectById(id);
    }

    @Override
    public List<FarmModelDO> getFarmModelList(Collection<Long> ids) {
        return farmModelMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<FarmModelDO> getFarmModelPage(FarmModelPageReqVO pageReqVO) {
        return farmModelMapper.selectPage(pageReqVO);
    }

    @Override
    public List<FarmModelDO> getFarmModelList(FarmModelExportReqVO exportReqVO) {
        return farmModelMapper.selectList(exportReqVO);
    }

}
